package com.example.lenovo_.healthyangel_menus;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.lenovo_.healthyangel.R;

/**
 * Created by Administrator on 2016/10/19.
 */

public class Personal_Infor_EditActivity extends AppCompatActivity {
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText et4;
    private RadioGroup rg1;
    private RadioGroup rg2;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private RadioButton rb5;
    private RadioButton rb6;
    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private CheckBox cb4;
    private CheckBox cb5;
    private Button bt;
    String shape;
    String taste=new String();
    int flag_bt1=0;
    int flag_bt2=0;
    int flag_bt3=0;
    int flag_bt4=0;
    int flag_bt5=0;
    int flag_bt6=0;
    int flag_cb1=0;
    int flag_cb2=0;
    int flag_cb3=0;
    int flag_cb4=0;
    int flag_cb5=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_infor_edit);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("修改个人信息");
        setSupportActionBar(toolbar);

        et1 = (EditText) findViewById(R.id.personal_infor_edit_name2);
        et2 = (EditText) findViewById(R.id.personal_infor_edit_height2);
        et3 = (EditText) findViewById(R.id.personal_infor_edit_weight2);
        et4 = (EditText) findViewById(R.id.personal_infor_edit_other2);
        rg1 = (RadioGroup) findViewById(R.id.personal_infor_edit_shape2);
        rg2 = (RadioGroup) findViewById(R.id.personal_infor_edit_shape3);
        rb1 = (RadioButton) findViewById(R.id.shape_button1);
        rb2 = (RadioButton) findViewById(R.id.shape_button2);
        rb3 = (RadioButton) findViewById(R.id.shape_button3);
        rb4 = (RadioButton) findViewById(R.id.shape_button4);
        rb5 = (RadioButton) findViewById(R.id.shape_button5);
        rb6 = (RadioButton) findViewById(R.id.shape_button6);
        cb1 = (CheckBox) findViewById(R.id.taste_checkbox1);
        cb2 = (CheckBox) findViewById(R.id.taste_checkbox2);
        cb3 = (CheckBox) findViewById(R.id.taste_checkbox3);
        cb4 = (CheckBox) findViewById(R.id.taste_checkbox4);
        cb5 = (CheckBox) findViewById(R.id.taste_checkbox5);
        bt = (Button) findViewById(R.id.personal_infor_edit_button);

        RadioGroupListener listener = new RadioGroupListener();
        rg1.setOnCheckedChangeListener(listener);//给rg1对象绑定监听器
        rg2.setOnCheckedChangeListener(listener);//给rg1对象绑定监听器

        CheckBoxListener listener1=new CheckBoxListener();
        cb1.setOnClickListener(listener1);
        cb2.setOnClickListener(listener1);
        cb3.setOnClickListener(listener1);
        cb4.setOnClickListener(listener1);
        cb5.setOnClickListener(listener1);

        ButtonListener listener2=new ButtonListener();
        bt.setOnClickListener(listener2);

    }

    class RadioGroupListener implements OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId==R.id.shape_button1){
                shape="瘦弱";
                flag_bt1++;
                if(flag_bt1%2==1){
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                    rb5.setChecked(false);
                    rb6.setChecked(false);
                }
            }
            if(checkedId==R.id.shape_button2){
                shape="苗条";
                flag_bt2++;
                if(flag_bt2%2==1){
                    rb1.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                    rb5.setChecked(false);
                    rb6.setChecked(false);
                }
            }
            if(checkedId==R.id.shape_button3){
                shape="匀称";
                flag_bt3++;
                if(flag_bt3%2==1){
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb4.setChecked(false);
                    rb5.setChecked(false);
                    rb6.setChecked(false);
                }
            }
            if(checkedId==R.id.shape_button4){
                shape="健壮";
                flag_bt4++;
                if(flag_bt4%2==1){
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb5.setChecked(false);
                    rb6.setChecked(false);
                }
            }
            if(checkedId==R.id.shape_button5){
                shape="微胖";
                flag_bt5++;
                if(flag_bt5%2==1){
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                    rb6.setChecked(false);
                }
            }
            if(checkedId==R.id.shape_button6){
                shape="肥胖";
                flag_bt6++;
                if(flag_bt6%2==1){
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                    rb5.setChecked(false);
                }
            }
        }
    }

    class CheckBoxListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.taste_checkbox1:
                    flag_cb1++;
                    if(flag_cb1%2==1) {
                        String taste1="酸  ";
                        taste=taste+taste1;
                    }
                    else
                        taste=taste.replace("酸  ","");
                    break;
                case R.id.taste_checkbox2:
                    flag_cb2++;
                    if(flag_cb2%2==1) {
                        String taste2="甜  ";
                        taste=taste+taste2;
                    }
                    else
                        taste=taste.replace("甜  ","");
                    break;
                case R.id.taste_checkbox3:
                    flag_cb3++;
                    if(flag_cb3%2==1) {
                        String taste3="苦  ";
                        taste=taste+taste3;
                    }
                    else
                        taste=taste.replace("苦  ","");
                    break;
                case R.id.taste_checkbox4:
                    flag_cb4++;
                    if(flag_cb4%2==1) {
                        String taste4 = "辣  ";
                        taste = taste + taste4;
                    }
                    else
                        taste=taste.replace("辣  ","");
                    break;
                case R.id.taste_checkbox5:
                    flag_cb5++;
                    if(flag_cb5%2==1) {
                        String taste5="咸  ";
                        taste=taste+taste5;
                    }
                    else
                        taste=taste.replace("咸  ","");
                    break;
            }
        }
    }
    class ButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent data=new Intent();
            String name=et1.getText().toString();
            String height=et2.getText().toString();
            String weight=et3.getText().toString();
            String other=et4.getText().toString();
            data.putExtra("data1",name);
            data.putExtra("data2",height);
            data.putExtra("data3",weight);
            data.putExtra("data4",other);
            data.putExtra("data5",shape);
            data.putExtra("data6",taste);
            setResult(2,data);
            finish();
        }
    }

}

